<?php
/**
 * 得到页面完整的URL
 * Enter description here ...
 */
function curPageURL() 
{
    $pageURL = 'http';

    if ($_SERVER["HTTPS"] == "on") 
    {
        $pageURL .= "s";
    }
    $pageURL .= "://";

    if ($_SERVER["SERVER_PORT"] != "80") 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
function getSysTitle(){
	return C("WEB_SYSTEM_NAME");
}
//检测内存使用情况
function my_memory_get_usage()
{
	if(function_exists("memory_get_usage")){
		return memory_get_usage();
	}
       //If its Windows
       //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
       //Doesn't work for 2000
       //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
       if ( substr(PHP_OS,0,3) == 'WIN') 
       {
               $output = array();
               exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
       
               return preg_replace( '/[\D]/', '', $output[5] ) * 1024;            
       }
       else{
           //We now assume the OS is UNIX
           //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
           //This should work on most UNIX systems
           $pid = getmypid();
           exec("ps -eo%mem,rss,pid | grep $pid", $output);
           $output = explode("  ", $output[0]);
           //rss is given in 1024 byte units
           return $output[1] * 1024;
       }
       /**
        * echo "<pre>";
		print_r(printf("memory usage: %01.2f MB", my_memory_get_usage()/1024/1024));
		echo "</pre>";
		exit;
        */
}
/**
 * 取文件的扩展名
 * Enter description here ...
 * @param unknown_type $file
 */
function getFileExt($file){
	return substr(strrchr($file, '.'), 1);
}
/**
 * 返回不包括扩展名的地方
 * Enter description here ...
 * @param unknown_type $file
 */
function getFileNoExt($file){
	$__end=strrpos($file, getFileExt($file));
	if($__end){
		return substr($file,0,$__end-1);
	}else{
		return $file;
	}
}

function DateFormat(  $format ,$timestamp){
		if (!$timestamp){
			return '';
		}
		if( is_numeric($timestamp) ){
			if ($format){
				if($format == 3){
					return date( "Y-m-d H:i:s", $timestamp );		
				}
				if( strlen($format) < 2 ){
					return date( "Y-m-d", $timestamp );
				}else{
					return date( $format, $timestamp );	
				}
			}
			return date( "Y-m-d H:i", $timestamp );			
		}else{
			return $timestamp;
		}
}
/**
 * 后台下单发送短信
 * Enter description here ...
 * @param unknown_type $mobile
 * @param unknown_type $product
 * @param unknown_type $pwd
 */
function sendAdminMobile($mobile,$product,$pwd){
	if($pwd){
		$__content=C("CR_ADMIN_MOBILE");
		$t=sprintf($__content,$product,$pwd);
	}else{
		$__content=C("CR_ADMIN_MOBILE_NOPWD");
		$t=sprintf($__content,$product);
	}
	Mobile::getMobile()->send($mobile,$t);
}
/**
 * 后台下单发送邮件
 * Enter description here ...
 * @param unknown_type $email
 * @param unknown_type $title
 * @param unknown_type $product
 * @param unknown_type $pwd
 */
function sendAdminMail($email,$title,$product,$pwd){
	if($pwd){
		$__content=C("CR_ADMIN_EMAIL");
		$t=sprintf($__content,$product,$pwd);
	}else{
		$__content=C("CR_HOME_EMAIL");
		$t=sprintf($__content,$product);
	}
	
	Mail::getMail()->sendMail($email,$title,$t);
}
/**
 * 指派服务队时发送短信
 * Enter description here ...
 * @param unknown_type $mobile
 * @param unknown_type $user
 * @param unknown_type $time
 */
function sendSrvMobile($mobile,$user,$time){
	$__content=C("SRV_MOBILE");
	$t=sprintf($__content,$user,$time);
	Mobile::getMobile()->send($mobile,$t);
}
/**
 * 指派服务队时发送邮件
 * Enter description here ...
 * @param unknown_type $mobile
 * @param unknown_type $user
 * @param unknown_type $time
 */
function sendSrvEmail($email,$user,$time,$contentText){
	$__content=C("SRV_EMAIL_DETAILS");
	$t=sprintf($__content,$user,$time,$contentText);
	Mail::getMail()->sendMail($email,'佳空间：处理服务单',$t);
}

function message($title,$url=null){
	if($url){
		$__url=$url;
	}else{
		$__url=curPageURL();
	}
	
	header("Location: ?g=Mobile&m=Index&a=message&title=".urlencode($title)."&url=".urlencode($__url));
	exit;
}
/**
 * 与当前时间进行比较  剩余多长时间
 * Enter description here ...
 * @param unknown_type $__time
 */
function getSyTime($__time){
	if(!$__time){
		return '无';
	}
	$c=$__time-time();
	
	if($c>0){
		$_1=floor($c/86400);
		
		//满足一天
		if($_1>=1){
			//取天
			$__d=intervalCalc($__time);
			$__y="0.".($c%86400);
			//取小时
			$__h=floor($__y*24);
			return "<span style='color:#FFFF00'>余".$__d."天".$__h."小时　</span>";
		}else {
			//$__h=intervalCalc($__time);
			$__h=floor($c/3600);
			if($__h>=1){
				return "<span style='color:#FFFF00'>余".$__h."小时　</span>";
			}else{
				//精确到分钟
				$__h=floor($c/60);
				return "<span style='color:#FFFF00'>余".$__h."分钟　</span>";
			}
			
		}
	}else{
		//超期
		$c=abs($c);
		$_1=floor($c/86400);
		
		
		
		if($_1>=1){
			//取天
			$__d=intervalCalc($__time);
			$__y="0.".($c%86400);
			//return $__y;
			//取小时
			$__h=floor($__y*24);
			return "<span style='color:#FF3300'>超期".$__d."天".$__h."小时　</span>";
		}else {
			//$__h=intervalCalc($__time);
			$__h=floor($c/3600);
			if($__h>=1){
				return "<span style='color:#FF3300'>超期".$__h."小时　</span>";
			}else{
				//精确到分钟
				$__h=floor($c/60);
				return "<span style='color:#FF3300'>超期".$__h."分钟　</span>";
			}
		}
	}
}
/**
 * 与当前时间进行比较  剩余多长时间
 * Enter description here ...
 * @param unknown_type $__time
 */
function getCreateTime($__time){
	if(!$__time){
		return '无';
	}
	$c=time()-$__time;
	
	$_1=floor($c/86400);
		
	//满足一天
	if($_1>=1){
		//取天
		$interval_secs=$c;
		$__d=(($interval_secs - ($interval_secs%86400))/86400);
		$__y="0.".($c%86400);
		//取小时
		$__h=floor($__y*24);
		return "<span style='color:#30960D'>".$__d."天".$__h."小时前　</span>";
	}else {
		//$__h=intervalCalc($__time);
		$__h=floor($c/3600);
		
		if($__h>=1){
			return "<span style='color:#30960D'>".$__h."小时前　</span>";
		}else{
			//精确到分钟
			$__h=floor($c/60);
			return "<span style='color:#30960D'>".$__h."分钟前　</span>";
		}
	}
}
function intervalCalc($postedtime)
{
   $now = time();
   $interval_secs =$postedtime-$now;
   if($interval_secs>0){
   	
   }else{
   	 $interval_secs=abs($interval_secs);
   }
   if ($interval_secs > 86400)
   { 
       $interval = (($interval_secs - ($interval_secs%86400))/86400);
       return $interval;
       //$interval.= " days ago";
   }
   elseif ($interval_secs > 3600)
   {
       $interval = ($interval_secs - ($interval_secs%3600)/3600);
      //return $interval;
       //$interval.= " hours ago";
   }
   elseif ($interval_secs > 60)
   {
       $interval = ($interval_secs - ($interval_secs%60)/60);
      // return $interval;
       //$interval.= " minutes ago";
   }
   
   return  $interval;
   
} 

function getZhezhao($postedtime){
	if(!$postedtime){
		return "<div style='width:160px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	}
	$now = time();
   	$interval_secs =$postedtime-$now;
   	if($interval_secs>0){
   		$_1=floor($interval_secs/86400);
   		if($_1>=1){
   			//return "<div style='width:121px;height:9px;background-color:#CCCCCC;padding-left:40px;'></div>";
   			return "<div style='width:0px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
   		}else{
   			$interval=floor($interval_secs/3600);
   			
   			if($interval<=4){
	       		return "<div style='width:119px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
	       }else if($interval>4 && $interval<=12){
	       		return "<div style='width:77px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
	       }
	   	   else if($interval>12 && $interval<=24){
	       		return "<div style='width:38px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
	       }
	       else if($interval>24){
	       		//return "<div style='width:121px;height:9px;background-color:#CCCCCC;margin-left:40px;'></div>";
	       		return "<div style='width:0px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
	       }
   		}
   	}else{
   		return "<div style='width:119px;height:9px;background-color:#CCCCCC;margin-left:1px;'></div>";
   	}
}

function getZhezhao1($postedtime){
	if(!$postedtime){
		return "<div style='width:160px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	}
	$now = time();
   	$interval_secs =$now-$postedtime;
   	if($interval_secs>0){
   		$_1=floor($interval_secs/86400);
   		
   		if($_1>=1){
   			return "<div style='width:119px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
   		}else{
   			$interval=floor($interval_secs/3600);
   			
   			if($interval<=4){
	       		//return "<div style='width:121px;height:9px;background-color:#CCCCCC;margin-left:40px;'></div>";
	       		return "<div style='width:0px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	       }else if($interval>4 && $interval<=12){
	       		return "<div style='width:38px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	       }
	   	   else if($interval>12 && $interval<=24){
	       		return "<div style='width:78px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	       }
	       else if($interval>24){
	       		return "<div style='width:119px;height:9px;background-color:#CCCCCC;margin-left:1px'></div>";
	       		
	       }
   		}
   	}
}

function getNumberValue($nm,$lre=''){
	if(is_numeric($nm)){
		return $nm.$lre;
	}else{
		return '无';
	}
}

function getYear(){
	//只取今年及下一年
	$__time=time();
	$__year=date("Y",$__time);
	$__nextYear=$__year+1;
	return array($__year,$__nextYear);
}

function getMonth(){
	$__rev=array();
	for($i=1;$i<=12;$i++){
		$__rev[]=$i;
	}
	return $__rev;
}

function getHours(){
	$__rev=array();
	for($i=1;$i<=23;$i++){
		$__rev[]=$i;
	}
	return $__rev;
}

function getMinutes(){
	$__rev=array();
	for($i=1;$i<=59;$i++){
		$__rev[]=$i;
	}
	return $__rev;
}

function getCurrentTime($type,$time=null){
	if($time){
		$__time=$time;
	}else{
		$__time=time();
	}
	
	$__t='';
	switch ($type){
		case 'y':
			$__t=date("Y",$__time);
			break;
		case 'm':
			$__t=date("m",$__time);
			break;
		case 'd':
			$__t=date("d",$__time);
			break;
		case 'h':
			$__t=date("H",$__time);
			break;
		case 'i':
			$__t=date("i",$__time);
			break;
	}
	return $__t;
}

function getPageList(){
	$__list=C("PAGELIST");
	$__CPAGE=$_REQUEST["upage"];
	$__html="";
	if(is_array($__list) && count($__list)){
		foreach ($__list as $v){
			if($__CPAGE==$v){
				$__html.='<span class="page2" onclick="toURL('.$v.');">'.$v.'</span>';
			}else{
				$__html.='<span class="page1" onclick="toURL('.$v.');">'.$v.'</span>';
			}
		}
	}
	return $__html;
}

function getOrderType($s){
	return ExUse::getOrderType($s);
}
/**
 * 根据用户ID 得到用户名称
 * Enter description here ...
 * @param unknown_type $u_id
 */
function getUserRName($u_id){
	if(!$u_id){
		return false;
	}
	$user=D("User");
	$s["U_ID"]=$u_id;
	$data=$user->where($s)->field("U_RNAME")->find();
	if($data){
		return $data["U_RNAME"]	;
	}else{
		return false;
	}
}
/**
 * 根据用户ID 得到用户会员名
 * Enter description here ...
 * @param unknown_type $u_id
 */
function getUserName($u_id){
	if(!$u_id){
		return false;
	}
	$user=D("User");
	$s["U_ID"]=$u_id;
	$data=$user->where($s)->field("U_NAME")->find();
	if($data){
		return $data["U_NAME"]	;
	}else{
		return false;
	}
}
/**
 * 谷歌地图坐标转百度地图坐标
 * Enter description here ...
 */
function GTOBMConvert($xy){
	if(!$xy){
		return false;
	}
	$__xy=explode(",", $xy);
	return array("x"=>$__xy[1],"y"=>$__xy[0]);
}
/**
 * 格式化费用   为了区别  0  null
 * Enter description here ...
 * @param unknown_type $v
 */
function getFormatMoney($v){
	if(is_numeric($v)){
		return $v;
	}else{
		return "无";
	}
}
/**
 * 得到radio中的值
 * Enter description here ...
 * @param unknown_type $v
 */
function getRadioValue($v){
	$arr=array("是","否");
	if($v>0){
		return $arr[0];
	}else{
		return $arr[1];
	}
}
/**
 * 根据类别取编号
 * Enter description here ...
 * @param unknown_type $v   值
 * @param unknown_type $tag 1:类型  2：型号
 */
function getProductNo($v,$tag=1){
	$p=D("Product");
	$field="P_CATEGORY_ID";
	if($tag==1){
		$s["P_CATEGORY"]=$v;
	}else{
		$s["P_MODEL"]=$v;
		$field="P_MODEL_ID";
	}
	$data=$p->where($s)->field($field)->find();
	if($data){
		return $data[$field];
	}else{
		return '';
	}
}
/**
 * 得到提交状态
 * Enter description here ...
 */
function getSumitHQ($v){
	if($v==1){
		return "已提交";
	}else{
		return "未提交";
	}
}
